package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupSevice;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author LiangTianQi
 * @describe
 * @date 2020/11/9 10:12
 */
@RestController
@RequestMapping("checkGroup")
public class CheckGroupController {

    @Reference
    private CheckGroupSevice checkGroupSevice;

    @PostMapping("/getByConditions")
    public Result getByConditions(@RequestBody QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        try {
            PageResult pageResult = checkGroupSevice.getByConditions(queryPageBean);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
    //添加检查组
    @PostMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        //调用service传入参数
        boolean flag = false;
        try {
            checkGroupSevice.add(checkGroup, checkitemIds);
            return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    @GetMapping("/get")
    public Result get(Integer id) {
        CheckGroup checkGroup = null;
        try {
            checkGroup = checkGroupSevice.get(id);
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS,checkGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        }
    }

    /**
     * 查询所有关联的中间表数据
     * @param id
     * @return
     */
    @GetMapping("/getCheckItemIdsById")
    public Result getCheckItemIdsById(Integer id) {

        try {
            List<Integer> checkItemIds = checkGroupSevice.getCheckItemIdsById(id);
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS,checkItemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        }
    }

    /**
     * 更新检查组，还有中间表的关系
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        //调用service传入参数
        try {
            checkGroupSevice.update(checkGroup, checkitemIds);
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

    /**
     * 删除检查组，并且删除中间表信息
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public Result delete(Integer id) {
        boolean flag = false;
        try {
            flag = checkGroupSevice.delete(id);
            if (flag) {
                return new Result(flag,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
            }else{
                return new Result(flag,MessageConstant.DELETE_CHECKGROUP_FAIL);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(flag,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }

    /**
     * 获取全部的检查组的信息
     * @return
     */
    @GetMapping("/getAll")
    public Result getAll() {

        try {
            List<CheckGroup> checkGroups = checkGroupSevice.getAll();
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroups);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

}
